package com.sinodata.ns.sv;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.group.GroupMapScript;
import jt.classic.system.group.IGroup;
import jt.classic.system.log.LogManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserCenter;
import jt.classic.system.user.UserManager;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.EncryptTool;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 监理人员管理控制器
 *
 * @author mido
 *
 */
public class SupervisorAction extends SimpleAction {
    /**
    * 查询列表
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return ActionForward
    * @throws Exception
    */
    public ActionForward querylist(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String start = StringTool.checkString(param.get("start"), "0");

        String size = StringTool.checkString(param.get("limit"), "20");

        pageNO = String.valueOf((Integer.parseInt(start) / Integer.parseInt(
                    size)) + 1);

        String sql1 = "SELECT COUNT(DISTINCT(APP_NS_JLRY.NU_ID)) FROM APP_NS_JLRY,ISYS_USER,ISYS_DEPT_USER ";

        String sql2 = "SELECT DISTINCT(APP_NS_JLRY.NU_ID) FROM APP_NS_JLRY,ISYS_USER,ISYS_DEPT_USER ";

        StringBuffer where = new StringBuffer(
                "WHERE ISYS_USER.ID = APP_NS_JLRY.NU_USERID AND ISYS_DEPT_USER.USER_ID = ISYS_USER.ID ");

        if (ISystem.matchrole(user, "SupervisorManager")) {
            where.append(" AND ISYS_DEPT_USER.DEPT_ID = '" +
                user.igroup().id() + "'");
        } else if (ISystem.matchrole(user, "NSManager")) {
            //query all
        } else {
            where.append(" AND 1 = -1 ");
        }

        String cname = StringTool.checkString(param.get("cname"));

        if (!cname.equals("")) {
            where.append(" AND ISYS_USER.NAME LIKE '%" + cname + "%' ");
        }

        String cmobile = StringTool.checkString(param.get("cmobile"));

        if (!cmobile.equals("")) {
            where.append(" AND ISYS_USER.PHONE LIKE '%" + cmobile + "%' ");
        }

        String cmobilecode = StringTool.checkString(param.get("cmobilecode"));

        if (!cmobilecode.equals("")) {
            where.append(" AND APP_NS_JLRY.NU_MOBILECODE LIKE '%" +
                cmobilecode + "%' ");
        }

        String nubdid = StringTool.checkString(param.get("nubdid"));

        if (!nubdid.equals("")) {
            where.append(" AND APP_NS_JLRY.NU_BDID = " + nubdid + " ");
        }

        String deptid = StringTool.checkString(param.get("deptid"));

        if (!deptid.equals("")) {
            where.append(" AND ISYS_DEPT_USER.DEPT_ID = '" + deptid + "' ");
        }

        String url = jt.classic.system.ISystem.getContextPath() +
            "/app/svaction.do?method=list";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            /*分页参数*/
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1,
                sql1 + where.toString());
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2,
                sql2 + where.toString());
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, size);
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            if ((resultList != null) && !resultList.isEmpty()) {
                SupervisorManager sm = new SupervisorManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    Map info = (Map) resultList.get(i);

                    String id = (String) info.get("NU_ID");

                    ISupervisor sv = sm.getSupervisorById(id);

                    IUser tempUser = sv.iuser();

                    info.put("NU_BD", (sv.ibd() != null) ? sv.ibd().iname() : "");
                    info.put("NU_BDID",
                        ((sv.ibd() != null) ? sv.ibd().id() : ""));
                    info.put("NU_USERID", tempUser.id());
                    info.put("NU_GC",
                        (sv.ibd() != null) ? sv.ibd().ibdname() : "");
                    info.put("NU_MOBILECODE", sv.imobilecode());
                    info.put("NAME", tempUser.iname());
                    info.put("JOB", tempUser.ijob());
                    info.put("PHONE", tempUser.iphone());
                    info.put("SEX", tempUser.isex());
                    info.put("GROUPNAME", tempUser.igroup().iname());
                    info.put("VALID", tempUser.iflag());
                }
            }

            /*标签*/
            Label label = plus.getLabel();

            JSONArray jsonArray = JSONArray.fromObject(resultList);

            String jsonString = "{\"pageNO\":\"" + label.current() +
                "\",\"totalPage\":\"" + label.totalfan() +
                "\",\"totalCount\":\"" + label.totaldata() +
                "\",\"queryList\":";
            jsonString += jsonArray.toString();
            jsonString += "}";

            request.setAttribute("result", jsonString);
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("jsonresult");
    }

    /**
     * 管理列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String start = StringTool.checkString(param.get("start"), "0");

        String size = StringTool.checkString(param.get("limit"), "20");

        pageNO = String.valueOf((Integer.parseInt(start) / Integer.parseInt(
                    size)) + 1);

        String sql1 = "SELECT COUNT(DISTINCT(APP_NS_JLRY.NU_ID)) FROM APP_NS_JLRY,ISYS_USER,ISYS_DEPT_USER ";

        String sql2 = "SELECT DISTINCT(APP_NS_JLRY.NU_ID) FROM APP_NS_JLRY,ISYS_USER,ISYS_DEPT_USER ";

        StringBuffer where = new StringBuffer(
                "WHERE ISYS_USER.ID = APP_NS_JLRY.NU_USERID AND ISYS_DEPT_USER.USER_ID = ISYS_USER.ID ");

        if (ISystem.matchrole(user, "SupervisorManager") ||
                ISystem.matchrole(user, "NSManager")) {
            where.append(" AND ISYS_DEPT_USER.DEPT_ID = '" +
                user.igroup().id() + "'");
        } else {
            where.append(" AND 1 = -1 ");
        }

        String cname = StringTool.checkString(param.get("cname"));

        if (!cname.equals("")) {
            where.append(" AND ISYS_USER.NAME LIKE '%" + cname + "%' ");
        }

        String cmobile = StringTool.checkString(param.get("cmobile"));

        if (!cmobile.equals("")) {
            where.append(" AND ISYS_USER.PHONE LIKE '%" + cmobile + "%' ");
        }

        String cmobilecode = StringTool.checkString(param.get("cmobilecode"));

        if (!cmobilecode.equals("")) {
            where.append(" AND APP_NS_JLRY.NU_MOBILECODE LIKE '%" +
                cmobilecode + "%' ");
        }

        String nubdid = StringTool.checkString(param.get("nubdid"));

        if (!nubdid.equals("")) {
            where.append(" AND APP_NS_JLRY.NU_BDID = " + nubdid + " ");
        }

        String url = jt.classic.system.ISystem.getContextPath() +
            "/app/svaction.do?method=list";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            /*分页参数*/
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1,
                sql1 + where.toString());
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2,
                sql2 + where.toString());
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, size);
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            if ((resultList != null) && !resultList.isEmpty()) {
                SupervisorManager sm = new SupervisorManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    Map info = (Map) resultList.get(i);

                    String id = (String) info.get("NU_ID");

                    ISupervisor sv = sm.getSupervisorById(id);

                    IUser tempUser = sv.iuser();

                    info.put("NU_BD", (sv.ibd() != null) ? sv.ibd().iname() : "");
                    info.put("NU_BDID",
                        ((sv.ibd() != null) ? sv.ibd().id() : ""));
                    info.put("NU_USERID", tempUser.id());
                    info.put("NU_GC",
                        (sv.icellbd() != null)
                        ? (sv.icellbd().icell().iname() + ":" +
                        sv.icellbd().iname()) : "");
                    info.put("NU_MOBILECODE", sv.imobilecode());
                    info.put("NAME", tempUser.iname());
                    info.put("JOB", tempUser.ijob());
                    info.put("PHONE", tempUser.iphone());
                    info.put("SEX", tempUser.isex());
                    info.put("GROUPNAME", tempUser.igroup().iname());
                    info.put("VALID", tempUser.iflag());
                    info.put("RATING", tempUser.irating());
                }
            }

            /*标签*/
            Label label = plus.getLabel();

            JSONArray jsonArray = JSONArray.fromObject(resultList);

            String jsonString = "{\"pageNO\":\"" + label.current() +
                "\",\"totalPage\":\"" + label.totalfan() +
                "\",\"totalCount\":\"" + label.totaldata() +
                "\",\"queryList\":";
            jsonString += jsonArray.toString();
            jsonString += "}";

            request.setAttribute("result", jsonString);
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("jsonresult");
    }

    /**
     * 编辑监理人员信息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = mapping.findForward("result");

        IUser user = ISystem.getSessionUser(request);

        if (ISystem.matchrole(user, "SupervisorManager") ||
                ISystem.matchrole(user, "NSManager")) {
            CommonBean4HSQ req = new CommonBean4HSQ(request);

            Map param = req.getResource();

            String id = (String) param.get("id");

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                SupervisorManager sm = new SupervisorManager(conn);

                ISupervisor sv = sm.getSupervisorById(id);

                request.setAttribute("sv", sv);

                forward = mapping.findForward("edit");
            } catch (Exception ex) {
                request.setAttribute("result", "{success: false}");
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            request.setAttribute("result", "{success: false, errCode: 0}");
        }

        return forward;
    }

    /**
     * 新增监理人员
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        if (ISystem.matchrole(user, "SupervisorManager") ||
                ISystem.matchrole(user, "NSManager")) {
            CommonBean4HSQ req = new CommonBean4HSQ(request);

            Map param = req.getResource();

            String logid = (String) param.get("logid");

            String code = (String) param.get("numcode");

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                UserManager um = new UserManager(conn);

                if (um.duplicateLogID(logid)) {
                    request.setAttribute("result",
                        "{success: false, errCode: -1}");
                } else {
                    SupervisorManager sm = new SupervisorManager(conn);

                    if (sm.codeInUserd(code)) {
                        request.setAttribute("result",
                            "{success: false, errCode: -2}");
                    } else {
                        BaseworkUtil util = new BaseworkUtil();

                        SimpleBean4Table userBean = new SimpleBean4TableImpl("ISYS_USER",
                                req);

                        userBean.getResource().put("flag", "0");

                        String pwd = StringTool.checkString(param.get("pwd"));

                        /*MD5加密*/
                        String encryptPwd = EncryptTool.encryptString(pwd);

                        /*设置用户密码*/
                        userBean.getResource().put("pwd", encryptPwd);

                        Map result1 = (Map) util.manualSave1(userBean, conn);

                        String userId = (String) result1.get("ID");

                        SimpleBean4Table duBean = new SimpleBean4TableImpl("ISYS_DEPT_USER",
                                req);
                        duBean.getResource().put("gid", user.igroup().id());
                        duBean.getResource().put("uid", userId);
                        duBean.getResource().put("upt", "0");
                        duBean.getResource().put("upj", "");
                        duBean.getResource().put("upr", param.get("rating"));
                        duBean.getResource().put("upo", "0");

                        util.manualSave1(duBean, conn);

                        SimpleBean4Table svBean = new SimpleBean4TableImpl("APP_NS_JLRY",
                                req);
                        svBean.getResource().put("nuserid", userId);

                        Map result = (Map) util.manualSave1(svBean, conn);

                        /*新监管人员主键ID*/
                        String rid = (String) result.get("NU_ID");

                        /*监理标段名称*/
                        String nubd = (String) param.get("nubd");
                        result.put("nubd", nubd);

                        /*监理标段id*/
                        String nubdid = (String) param.get("nubdid");
                        result.put("nubdid", nubdid);

                        /*if (!nubdid.equals("")) {
                            String[] nubdids = nubdid.split(",");
                            String[] nubds = nubd.split(",");

                            for (int i = 0; i < nubdids.length; i++) {
                                SimpleBean4Table jlbdBean = new SimpleBean4TableImpl(
                                        "APP_NS_JLBD");
                                jlbdBean.getResource().put("nubdid", nubdids[i]);
                                jlbdBean.getResource().put("nubd", nubds[i]);
                                jlbdBean.getResource().put("rid", rid);

                                Map jlbdResult = (Map) util.manualSave1(jlbdBean,
                                        conn);
                            }
                        }*/
                        result.putAll(result1);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, request.getRemoteAddr(), "", "",
                            "addSupervisor", "insert", result.toString());

                        conn.commit();

                        IUser newUser = um.getUserById(userId);

                        UserCenter uc = UserCenter.getInstance();
                        uc.reset(newUser);

                        List list = user.iallgroup();

                        if ((list != null) && !list.isEmpty()) {
                            GroupMapScript gms = GroupMapScript.getInstance();

                            for (int i = 0; i < list.size(); i++) {
                                IGroup group = (IGroup) list.get(i);
                                gms.setUpdate(group.id());
                            }
                        }

                        request.setAttribute("result", "{success: true}");
                    }
                }
            } catch (Exception ex) {
                conn.rollback();
                request.setAttribute("result", "{success: false, errCode: -3}");
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            request.setAttribute("result", "{success: false, errCode: 0}");
        }

        return mapping.findForward("result");
    }

    /**
     * 修改监理人员
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward update(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        if (ISystem.matchrole(user, "SupervisorManager") ||
                ISystem.matchrole(user, "NSManager")) {
            CommonBean4HSQ req = new CommonBean4HSQ(request);

            Map param = req.getResource();

            String id = (String) param.get("rid");

            String code = (String) param.get("numcode");

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                SupervisorManager sm = new SupervisorManager(conn);

                ISupervisor sv = sm.getSupervisorById(id);

                /*修改了设备编号*/
                if (!sv.imobilecode().equals(code) && sm.codeInUserd(code)) {
                    request.setAttribute("result",
                        "{success: false, errCode: -2}");
                } else {
                    BaseworkUtil util = new BaseworkUtil();

                    SimpleBean4Table userBean = new SimpleBean4TableImpl("ISYS_USER",
                            req);

                    /*设置用户ID*/
                    userBean.getResource().put("uid", sv.iuser().id());

                    Map result1 = (Map) util.manualUpdate1(userBean, conn);

                    SimpleBean4Table duBean = new SimpleBean4TableImpl("ISYS_DEPT_USER",
                            req);
                    duBean.getResource().put("gid", sv.iuser().igroup().id());
                    duBean.getResource().put("uid", sv.iuser().id());
                    duBean.getResource().put("upr", param.get("rating"));

                    Map result2 = (Map) util.manualUpdate1(duBean, conn);

                    SimpleBean4Table svBean = new SimpleBean4TableImpl("APP_NS_JLRY",
                            req);

                    Map result = (Map) util.manualUpdate1(svBean, conn);

                    /*监管人员主键ID*/
                    String rid = (String) result.get("NU_ID");

                    result.putAll(result1);
                    result.putAll(result2);

                    /*原监理单元*/
                    String cellid = ((sv.ibd() != null) ? sv.ibd().id() : "");

                    /*监理标段发生变化*/
                    /*if (!param.get("nucellid").equals(cellid)) {
                        SimpleBean4Table jlbdBean = new SimpleBean4TableImpl("APP_NS_JLBD",
                                req);

                        util.manualDelete1(jlbdBean, conn);

                        监理标段名称
                        String nubd = (String) param.get("nubd");
                        result.put("nubd", nubd);

                        监理标段id
                        String nubdid = (String) param.get("nubdid");
                        result.put("nubdid", nubdid);

                        if (!nubdid.equals("")) {
                            String[] nubdids = nubdid.split(",");
                            String[] nubds = nubd.split(",");

                            for (int i = 0; i < nubdids.length; i++) {
                                SimpleBean4Table jlbdBean_ = new SimpleBean4TableImpl(
                                        "APP_NS_JLBD");
                                jlbdBean_.getResource().put("nubdid", nubdids[i]);
                                jlbdBean_.getResource().put("nubd", nubds[i]);
                                jlbdBean_.getResource().put("rid", rid);

                                util.manualSave1(jlbdBean_, conn);
                            }
                        }
                    }*/
                    LogManager lm = new LogManager(conn);

                    lm.log(user, request.getRemoteAddr(), "", "",
                        "updateSupervisor", "update", result.toString());

                    conn.commit();

                    UserManager um = new UserManager(conn);

                    IUser newUser = um.getUserById(sv.iuser().id());

                    UserCenter uc = UserCenter.getInstance();
                    uc.reset(newUser);

                    List list = user.iallgroup();

                    if ((list != null) && !list.isEmpty()) {
                        GroupMapScript gms = GroupMapScript.getInstance();

                        for (int i = 0; i < list.size(); i++) {
                            IGroup group = (IGroup) list.get(i);
                            gms.setUpdate(group.id());
                        }
                    }

                    request.setAttribute("result", "{success: true}");
                }
            } catch (Exception ex) {
                conn.rollback();
                request.setAttribute("result", "{success: false}");
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            request.setAttribute("result", "{success: false, errCode: 0}");
        }

        return mapping.findForward("result");
    }

    /**
     * 删除监理人员
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        if (ISystem.matchrole(user, "SupervisorManager") ||
                ISystem.matchrole(user, "NSManager")) {
            CommonBean4HSQ req = new CommonBean4HSQ(request);

            Map param = req.getResource();

            String id = (String) param.get("id");

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                SupervisorManager sm = new SupervisorManager(conn);

                ISupervisor sv = sm.getSupervisorById(id);

                sm.delete(sv);

                LogManager lm = new LogManager(conn);

                lm.log(user, request.getRemoteAddr(), "", "",
                    "deleteSupervisor", "delete", sv.getResource().toString());

                conn.commit();

                if ((sv.iuser().irating() == 1) || (sv.iuser().irating() == 2)) {
                    UserCenter uc = UserCenter.getInstance();
                    uc.remove(sv.iuser().id());

                    List list = sv.iuser().iallgroup();

                    if ((list != null) && !list.isEmpty()) {
                        GroupMapScript gms = GroupMapScript.getInstance();

                        for (int i = 0; i < list.size(); i++) {
                            IGroup group = (IGroup) list.get(i);
                            gms.setUpdate(group.id());
                        }
                    }
                }

                request.setAttribute("result", "{success: true}");
            } catch (Exception ex) {
                conn.rollback();
                request.setAttribute("result", "{success: false}");
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            request.setAttribute("result", "{success: false, errCode: 0}");
        }

        return mapping.findForward("result");
    }

    /**
     * 监理人员状态更新
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward flag(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        if (ISystem.matchrole(user, "SupervisorManager") ||
                ISystem.matchrole(user, "NSManager")) {
            CommonBean4HSQ req = new CommonBean4HSQ(request);

            Map param = req.getResource();

            String id = (String) param.get("id");

            String flag = (String) param.get("flag");

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                SupervisorManager sm = new SupervisorManager(conn);

                ISupervisor sv = sm.getSupervisorById(id);

                sm.flag(sv, Integer.parseInt(flag));

                conn.commit();

                UserManager um = new UserManager(conn);

                IUser u = um.getUserById(sv.iuser().id());

                UserCenter uc = UserCenter.getInstance();
                uc.reset(u);

                List list = u.iallgroup();

                if ((list != null) && !list.isEmpty()) {
                    GroupMapScript gms = GroupMapScript.getInstance();

                    for (int i = 0; i < list.size(); i++) {
                        IGroup group = (IGroup) list.get(i);
                        gms.setUpdate(group.id());
                    }
                }

                request.setAttribute("result", "{success: true}");
            } catch (Exception ex) {
                conn.rollback();
                request.setAttribute("result", "{success: false}");
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            request.setAttribute("result", "{success: false, errCode: 0}");
        }

        return mapping.findForward("result");
    }
}
